#include "CShortcutDialog.h"
#include "ui_CShortcutDialog.h"

CShortcutDialog::CShortcutDialog(QWidget* parent)
    : QDialog(parent),
      ui(new Ui::CShortcutDialog)
{
    ui->setupUi(this);
}

CShortcutDialog::~CShortcutDialog()
{
    delete ui;
}

void CShortcutDialog::on_btnOk_clicked()
{
    this->accept();
}

void CShortcutDialog::on_btnCancel_clicked()
{
    this->reject();
}

void CShortcutDialog::on_tbSelect_clicked()
{
    QString str;
    QString path = ui->lePath->text();
    if (m_bOpenFileDialog) {
        str = QFileDialog::getOpenFileName(this, tr("选择文件"), path.isEmpty() ? "" : QFileInfo(path).absolutePath(), tr("All Files (*)"));
        if (!str.isEmpty()) {
            ui->lePath->setText(str);
        }
    }
    else {
        str = QFileDialog::getExistingDirectory(this, tr("选择文件夹"), path.isEmpty() ? "" : path, QFileDialog::ShowDirsOnly);
        if (!str.isEmpty()) {
            ui->lePath->setText(str);
        }
    }

    if (!str.isEmpty() && ui->leName->text().isEmpty()) {
        QFileInfo info(str);
        ui->leName->setText(info.fileName());
    }
}

int CShortcutDialog::execMyDialog(bool isOpenFileDialog)
{
    return this->execMyDialog(nullptr, isOpenFileDialog);
}

int CShortcutDialog::execMyDialog(const CMyShortcut* shortcut, bool isOpenFileDialog)
{
    if (shortcut) {
        return execMyDialog(shortcut->getName(), shortcut->getPath(), isOpenFileDialog);
    }
    else {
        return execMyDialog("", "", isOpenFileDialog);
    }
}

int CShortcutDialog::execMyDialog(const QString& name, const QString& path, bool isOpenFileDialog)
{
    if (isOpenFileDialog) {
        this->setWindowTitle(tr("添加文件快捷方式"));
    }
    else {
        this->setWindowTitle(tr("添加文件夹快捷方式"));
    }

    ui->leName->setText(name);
    ui->lePath->setText(path);
    m_bOpenFileDialog = isOpenFileDialog;
    return this->exec();
}

QString CShortcutDialog::getName()
{
    return ui->leName->text();
}

QString CShortcutDialog::getPath()
{
    return ui->lePath->text();
}
